package com.github.wings27.interview.JavaCommonKnowledge;


import org.junit.Assert;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Project Name: common-interview-problems
 * Date Created: 2013/11/05 下午6:42
 *
 * @author wings
 */
@SuppressWarnings({"finally", "ReturnInsideFinallyBlock"})
public class TryCatchFinally {
    public static void main(String[] args) {
        TryCatchFinally tryCatchFinally = new TryCatchFinally();
        String result;

        result = tryCatchFinally.noException();
        System.out.println("noException: " + result);
        Assert.assertEquals("+try+finally", result);

        result = tryCatchFinally.multipleCatches();
        System.out.println("multipleCatches: " + result);
        Assert.assertEquals("+FileNotFoundException+finally+AfterTryCatch", result);

        result = tryCatchFinally.catchAndFinally();
        System.out.println("catchAndFinally: " + result);
        Assert.assertEquals("+catch", result);  // But the "finally" block still executed!

        result = tryCatchFinally.catchAndFinallyWithReturn();
        System.out.println("catchAndFinallyWithReturn: " + result);
        Assert.assertEquals("+catch+finally", result);
    }

    private String noException() {
        String result = "";
        try {
            result += "+try";
            return result;
        } catch (Exception e) {
            result += "+catch";
            return result;
        } finally {
            result += "+finally";
            return result;
        }
        /* Unreachable statement

        result += "+AfterTryCatch";
        return result;

        */
    }

    private String multipleCatches() {
        String result = "";
        try {
            new FileReader("no-such-text-file-haha.txt");
        } catch (FileNotFoundException e) {
            result += "+FileNotFoundException";
        } catch (IOException e) {
            result += "+IOException";
        } catch (Exception e) {
            result += "+Exception";
        } finally {
            result += "+finally";
        }
        result += "+AfterTryCatch";
        return result;
    }

    private String catchAndFinally() {
        String result = "";
        try {
            new FileReader("no-such-text-file-haha.txt");
        } catch (FileNotFoundException e) {
            result += "+catch";
            return result;
        } finally {
            result += "+finally";
            System.err.println("catchAndFinally: The finally block will always execute!");
        }
        result += "+AfterTryCatch";
        return result;
    }

    private String catchAndFinallyWithReturn() {
        String result = "";
        try {
            new FileReader("no-such-text-file-haha.txt");
        } catch (FileNotFoundException e) {
            result += "+catch";
            return result;
        } finally {
            result += "+finally";
            return result;
        }
        /* Unreachable statement

        result += "+AfterTryCatch";
        return result;

        */
    }
}
